/* Endless Code Copyright (C) 2009 Jingul,Kim
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
package ec.gfx.ui;

import java.util.LinkedList;

import ec.Result;
import ec.gfx.GL;
import ec.gfx.Node;
import ec.gfx.Scene;

/**
 * @since 2009
 * @version 1.0
 * @author <a href="mailto:jingul.kim@gmail.com">Jingul,Kim</a>
 */
public class Handle extends Node {
	private LinkedList<Dialog> packs = new LinkedList<Dialog>();
	
	public Handle() {
		Dialog d1 = new Dialog(this);
		Dialog d2 = new Dialog(this);
		Button btn = new Button();
		
		d1.setVisible(true);
		d2.setVisible(true);
		btn.setVisible(true);
		
		d2.setLocation(100, 150);
		d1.setLocation(150, 200);
		
		d2.add(btn);
		d2.setLocation(10f, 10f);
		
		d1.setTitle("Box1");
		d2.setTitle("Box2");
		d1.setSize(100, 50);
		d2.setSize(200, 150);
		packs.add(d1);
		packs.add(d2);
	}

	public void setFocused(Dialog box) {
		packs.remove(box);
		packs.add(box);
	}
	
	public Pack getFocused() {
		return packs.getFirst();
	}
	
	@Override
	public Result pre(Scene scene) {
		GL.disable(GL.GL_BLEND);
		GL.disable(GL.GL_TEXTURE_2D);
		
		return super.pre(scene);
	}
	
	@Override
	public Result render(Scene scene) {
		for (Dialog p : packs) {
			if (p.pre(scene) == Result.OK) {
				p.now(scene);
				p.render(scene);
			}
			p.post(scene);
		}
		
		return Result.OK;
	}
	
	@Override
	public Result update(Scene scene) {
		for(Pack p: packs) {
			p.update(scene);
		}
		return Result.OK;
	}
	
	public void push(Dialog pack) {
		packs.addFirst(pack);
	}
}
